package net.zhengxinyun.performance.service.indicators;

import net.zhengxinyun.performance.entity.DepartmentEntity;
import net.zhengxinyun.performance.entity.TownEntity;
import net.zhengxinyun.performance.entity.TownIndicatorsEntity;
import net.zhengxinyun.performance.filter.TownFilter;
import net.zhengxinyun.performance.mapper.Department.DepartmentEntityMapper;
import net.zhengxinyun.performance.mapper.Indicators.TownMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @Auther: hyp
 * @Date: 2018/10/29 18:25
 */
@Service
public class TownServiceImpl implements TownService {

    private TownMapper townMapper;
    private DepartmentEntityMapper departMapper;

    @Autowired
    public TownServiceImpl(TownMapper townMapper,DepartmentEntityMapper departMapper) {
        this.townMapper = townMapper;
        this.departMapper=departMapper;
    }

    @Override
    public int delete(TownEntity townEntity) {
        return townMapper.deleteByPrimaryKey(townEntity.getId());
    }

    @Override
    public int update(TownEntity townEntity) {
        return townMapper.updateByPrimaryKey(townEntity);
    }

    @Override
    public String insert(TownIndicatorsEntity townIndicatorsEntity,List<TownEntity> townList,String datatime) {
        Long lastId = townIndicatorsEntity.getId();
        for (TownEntity townEntity : townList) {
            townEntity.setIndexId(lastId.toString());

           /* System.out.println("---------------"+townEntity.getTownName()+"--------------------");
            if("福泉镇".equals(townEntity.getTownName().trim()))
            {
                townEntity.setTownName("富泉乡");
            }
            if("大洋乡".equals(townEntity.getTownName().trim()))
            {
                townEntity.setTownName("大洋镇");
            }
            if("红星镇".equals(townEntity.getTownName().trim()))
            {
                townEntity.setTownName("红星乡");
            }*/
            DepartmentEntity depart = departMapper.getDepartByName(townEntity.getTownName());

            townEntity.setDepartcode(depart.getDepartcode());  //设置部门编码
            townEntity.setDatetime(datatime);
            long a = townMapper.insert(townEntity);
            if (a != 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "添加失败,请联系管理员";
            }
        }
        return "添加成功";
    }

    @Override
    public List<TownFilter> query(TownFilter townFilter) {
        return townMapper.query(townFilter);
    }

    @Override
    public List<TownFilter> queryByIndexId(String indexId) {
        return townMapper.queryByIndexId(indexId);
    }
}
